package com.cnhis.cloudhealth.clinical.clidoctor.clideladvice.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.clidoctor.clideladvice.dao.CliDelAdviceDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

@Service
public class CliDelAdviceBo
{
    @Resource
    private CliDelAdviceDao cliDelAdviceDao;
    
    @Autowired
    private GetNewId getNewId;
    
    public ModelVo delclinicAdvice(Map map)
        throws Exception
    {
        // 获取业务分类编号
        Integer lacf01 = Integer.valueOf(map.get("lacf01").toString());
        
        // 如果不是住院业务
        if (!lacf01.equals(2))
        {
            return aDMzyw(map);
        }
        return null;
    }
    
    /**
     * 如果不是住院业务
     * 
     * @param map
     * @return
     * @throws Exception
     */
    @SuppressWarnings({"rawtypes"})
    private ModelVo aDMzyw(Map map)
        throws Exception
    {
        String lXml = map.get("lXml").toString();
        
        // 将Json数据装换为list
        List ls = JsonUtil.getList(lXml);
        
        // 根据医嘱ID获取医嘱详情
        List TmpVafdf = getAdviceDetailByID(ls);
        
        // 过滤VAF10 >= 3的医嘱信息   --描述:  1:新嘱;2:疑问;3:校对;4:作废;5:删除 ;6:暂停;7:启用;8:已发送或停止;9:确认停止;10:皮试结果
        TmpVafdf = getGtThree(TmpVafdf);
        
        if (TmpVafdf.size() == 0)
        {
            return new ModelVo(StaticKeys.OH_FALSE, "医嘱都已执行，没有可以删除的医嘱！");
        }
        
        // 医嘱单ID去重
        List TmpCBMall = getTmpCBMall(TmpVafdf);
        // 获取TmpCBMall对应的医嘱ID但却不在TmpVafdf中
        List TmpVAFdno = getTmpVAFdno(TmpCBMall, TmpVafdf);
        
        // 获取TmpCBMall对应的医嘱ID但却不在TmpVAFdno中
        List tmpCBMd = getTmpCBMdByTmpCBMall(TmpCBMall, TmpVAFdno);
        
        // 获取医嘱的申请单信息
        List tmpVBQd = getTmpVBQdByTmpVafdf(TmpVafdf, TmpVAFdno);
        
        String bce03 = map.get("BCE03").toString();
        
        return saveADMzyw(TmpVafdf, TmpCBMall, TmpVAFdno, tmpCBMd, tmpVBQd, bce03);
        
    }
    
    private ModelVo saveADMzyw(List tmpVafdf, List tmpCBMall, List tmpVAFdno, List tmpCBMd, List tmpVBQd, String bce03)
        throws Exception
    {
        String aDate = DateUtils.getSystemDate();
        // 记录医嘱删除操作日志
        List inVBG1s = getInVBG1s(tmpVafdf, aDate, bce03);
        
        insertDelVbg(inVBG1s);
        
        delMzCBM1(tmpCBMd);
        delMzVBQ1(tmpVBQd);
        delMzVAF1(tmpVafdf);
        delMzVAO1(tmpCBMd);
        
        // 医嘱删除医嘱单删除
        List inVbgDels = getInVbgDels(tmpCBMd, aDate, bce03);
        
        insertVbgDel(inVbgDels);
        
        // 删除病人医嘱路径
        delVCR1ByTmpVafdf(tmpVafdf);
        
        // 更新病人医嘱提醒状态
        updateVBZ1ByTmpVafdf(tmpVafdf);
        
        return new ModelVo(StaticKeys.OH_SUCCESS, "");
    }
    
    private void updateVBZ1ByTmpVafdf(List tmpVafdf)
        throws Exception
    {
        cliDelAdviceDao.updateVBZ1ByTmpVafdf(tmpVafdf);
    }
    
    private void delVCR1ByTmpVafdf(List tmpVafdf)
        throws Exception
    {
        cliDelAdviceDao.delVCR1ByTmpVafdf(tmpVafdf);
    }
    
    private void insertVbgDel(List inVBG1s)
        throws Exception
    {
        cliDelAdviceDao.insertVbgDel(inVBG1s);
    }
    
    private List getInVbgDels(List tmpCBMd, String aDate, String bce03)
    {
        int tc = tmpCBMd.size();
        List ls = getNewId.nextId(tc);
        
        Map map = null;
        for (int i = 0; i < tc; i++)
        {
            map = (Map)tmpCBMd.get(i);
            map.put("vbg01", ls.get(i));
            // map.put("vbg03",new Integer(0));
            map.put("bce03", bce03);
            map.put("vbg05", aDate);
            map.put("vbg06", "医嘱删除医嘱单删除");
        }
        return tmpCBMd;
    }
    
    private void delMzVAO1(List tmpCBMd)
        throws Exception
    {
        if (tmpCBMd.size() > 0)
        {
            cliDelAdviceDao.delMzVAO1(tmpCBMd);
        }
    }
    
    private void delMzVAF1(List tmpVafdf)
        throws Exception
    {
        if (tmpVafdf.size() > 0)
        {
            cliDelAdviceDao.delMzVAF1(tmpVafdf);
        }
        
    }
    
    private void delMzVBQ1(List tmpVBQd)
        throws Exception
    {
        if (tmpVBQd.size() > 0)
        {
            cliDelAdviceDao.delMzVBQ1(tmpVBQd);
        }
    }
    
    private void delMzCBM1(List tmpCBMd)
        throws Exception
    {
        if (tmpCBMd.size() > 0)
        {
            cliDelAdviceDao.delMzCBM1(tmpCBMd);
        }
    }
    
    private void insertDelVbg(List inVBG1s)
        throws Exception
    {
        cliDelAdviceDao.insertDelVbg(inVBG1s);
    }
    
    private List getInVBG1s(List tmpVafdf, String aDate, String bce03)
    {
        int tv = tmpVafdf.size();
        List idS = getNewId.nextId(tv);
        Map map = null;
        for (int i = 0; i < tv; i++)
        {
            map = (Map)tmpVafdf.get(i);
            map.put("vbg01", idS.get(i));
            map.put("bce03", bce03);
            map.put("adate", aDate);
            map.put("vbg06", "医嘱删除操作");
        }
        
        return tmpVafdf;
    }
    
    private List getTmpVBQdByTmpVafdf(List tmpVafdf, List tmpVAFdno)
    {
        List ls = new ArrayList<>();
        
        int tv = tmpVAFdno.size();
        int tvf = tmpVafdf.size();
        
        Map map = null;
        String vaf51 = null;
        
        Set sets = new HashSet<>();
        
        // 获取tmpVAFdno的CBM01字段；
        Set set = new HashSet<>();
        for (int i = 0; i < tv; i++)
        {
            map = (Map)tmpVAFdno.get(i);
            if (map.containsKey("vaf51"))
            {
                vaf51 = map.get("vaf51").toString();
                if (vaf51 != null)
                {
                    set.add(vaf51);
                }
            }
        }
        
        // 获取结果集
        for (int i = 0; i < tvf; i++)
        {
            map = (Map)tmpVafdf.get(i);
            if (map.containsKey("vaf51"))
            {
                vaf51 = map.get("vaf51").toString();
                if (vaf51 != null)
                {
                    if (!set.contains(vaf51))
                    {
                        sets.add(vaf51);
                    }
                }
            }
            
        }
        
        for (Iterator it = sets.iterator(); it.hasNext();)
        {
            vaf51 = (String)it.next();
            map = new HashMap<>();
            map.put("vbq01", vaf51);
            ls.add(map);
        }
        
        return ls;
    }
    
    private List getTmpCBMdByTmpCBMall(List tmpCBMall, List tmpVAFdno)
    {
        
        List ls = new ArrayList<>();
        
        int tv = tmpVAFdno.size();
        int tc = tmpCBMall.size();
        
        Map map = null;
        Integer cbm01 = null;
        
        // 获取tmpVAFdno的CBM01字段；
        Set set = new HashSet<>();
        for (int i = 0; i < tv; i++)
        {
            map = (Map)tmpVAFdno.get(i);
            if (map.containsKey("cbm01"))
            {
                cbm01 = Integer.valueOf(map.get("cbm01").toString());
                if (cbm01 != null)
                {
                    set.add(cbm01);
                }
            }
        }
        
        // 获取结果集
        for (int i = 0; i < tc; i++)
        {
            map = (Map)tmpCBMall.get(i);
            if (map.containsKey("cbm01"))
            {
                cbm01 = Integer.valueOf(map.get("cbm01").toString());
                if (cbm01 != null)
                {
                    if (!set.contains(cbm01))
                    {
                        ls.add(map);
                    }
                }
            }
            
        }
        return ls;
    }
    
    /**
     * 获取TmpCBMall对应的医嘱ID但却不在TmpVafdf中
     * 
     * @param tmpCBMall
     * @param tmpVafdf
     * @return
     * @throws Exception
     */
    private List getTmpVAFdno(List tmpCBMall, List tmpVafdf)
        throws Exception
    {
        List vafs = getVafsByTmpCBMall(tmpCBMall);
        
        List ls = new ArrayList<>();
        
        int vf = vafs.size();
        int tv = tmpVafdf.size();
        
        Map map = null;
        Integer vaf01 = null;
        
        // 获取tmpVafdf的vaf01字段；
        Set set = new HashSet<>();
        for (int i = 0; i < tv; i++)
        {
            map = (Map)tmpVafdf.get(i);
            if (map.containsKey("vaf01"))
            {
                vaf01 = Integer.valueOf(map.get("vaf01").toString());
                if (vaf01 != null)
                {
                    set.add(vaf01);
                }
            }
        }
        
        // 获取结果集
        for (int i = 0; i < vf; i++)
        {
            map = (Map)vafs.get(i);
            if (map.containsKey("vaf01"))
            {
                vaf01 = Integer.valueOf(map.get("vaf01").toString());
                if (vaf01 != null)
                {
                    if (!set.contains(vaf01))
                    {
                        ls.add(map);
                    }
                }
            }
            
        }
        
        return ls;
    }
    
    private List getVafsByTmpCBMall(List tmpCBMall)
        throws Exception
    {
        return cliDelAdviceDao.getVafsByTmpCBMall(tmpCBMall);
    }
    
    private List getTmpCBMall(List tmpVafdf)
    {
        List ls = new ArrayList();
        
        // 去除医嘱单ID
        Set<Integer> set = new HashSet<Integer>();
        for (int i = 0; i < tmpVafdf.size(); i++)
        {
            Map map = (Map)tmpVafdf.get(i);
            if (map.containsKey("cbm01"))
            {
                Integer cbm01 = Integer.valueOf(map.get("cbm01").toString());
                if (cbm01 != null && !cbm01.equals(0))
                {
                    set.add(cbm01);
                }
            }
        }
        
        for (Integer i : set)
        {
            Map map = new HashMap<>();
            map.put("cbm01", i);
            ls.add(map);
        }
        
        return ls;
    }
    
    /**
     * 过滤VAF10 >= 3的医嘱信息
     * 
     * @param tmpVafdf
     * @return
     */
    private List getGtThree(List tmpVafdf)
    {
        if (tmpVafdf == null || tmpVafdf.size() == 0)
        {
            return new ArrayList();
        }
        
        List ls = new ArrayList();
        for (int i = 0; i < tmpVafdf.size(); i++)
        {
            Map map = (Map)tmpVafdf.get(i);
            if (map.containsKey("vaf10"))
            {
                Integer vaf10 = Integer.valueOf(map.get("vaf10").toString());
                vaf10 = vaf10 == null ? 0 : vaf10;
                if (vaf10 < 3)
                {
                    ls.add(map);
                }
            }
        }
        
        return ls;
    }
    
    private List getAdviceDetailByID(List ls)
        throws Exception
    {
        return cliDelAdviceDao.getAdviceDetailByID(ls);
    }
    
    private String getVAF59(Map map)
        throws Exception
    {
        return cliDelAdviceDao.getVAF59(map);
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo deleteAdviceList(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List listAll = new ArrayList();
        try
        {
            map.put("VAF59", 0);
            if ("2".equals(map.get("lVAF04").toString()))
            {
                String vaf59 = getVAF59(map);
                map.put("VAF59", vaf59);
            }
            List list = cliDelAdviceDao.deleteAdviceList(map);
            listAll.addAll(list);
            list = cliDelAdviceDao.deleteAdviceList1(map);
            listAll.addAll(list);
            list = cliDelAdviceDao.deleteAdviceList2(map);
            listAll.addAll(list);
            list = cliDelAdviceDao.deleteAdviceList3(map);
            listAll.addAll(list);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(listAll);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(listAll);
        return mo;
    }
    
    public ModelVo valuationSignDeleteAdvice(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List listAll = null;
        try
        {
            if ("1".equals(map.get("acf01").toString()))
            {
                listAll = cliDelAdviceDao.valuationSignDeleteAdvice1(map);
            }
            else
            {
                listAll = cliDelAdviceDao.valuationSignDeleteAdvice(map);
            }
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(listAll);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(listAll);
        return mo;
    }
    
    public ModelVo queryAdvicePreDeleteList(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            List listAll = cliDelAdviceDao.queryAdvicePreDeleteList(map);
            mo.setList(listAll);
            mo.setResult(StaticKeys.OH_SUCCESS);
            mo.setRows(listAll);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
    }
}
